<template>
  <el-card class="form-container" shadow="never">
    <el-form
      ref="merchantConfig"
      :model="merchantConfig"
      :rules="rules"
      label-width="150px"
    >
      <el-form-item label="店铺名称：" prop="name">
        <el-input v-model="merchantConfig.shopName" />
      </el-form-item>
      <el-form-item label="手机号码：">
        <el-input v-model="merchantConfig.phone" />
      </el-form-item>

      <el-form-item label="店铺LOGO：">
        <single-upload v-model="merchantConfig.logo" />
      </el-form-item>

      <el-form-item label="店铺地址：">
        <el-input v-model="merchantConfig.address" />
      </el-form-item>

      <el-form-item label="营业时间：">
        <el-input v-model="merchantConfig.openTime" />
      </el-form-item>

      <el-form-item label="店铺说明：">
        <el-input v-model="merchantConfig.notes" />
      </el-form-item>

      <el-form-item label="是否显示：">
        <el-radio-group v-model="merchantConfig.status">
          <el-radio :label="1">是</el-radio>
          <el-radio :label="0">否</el-radio>
        </el-radio-group>
      </el-form-item>


      <el-form-item>
        <el-button type="primary" @click="onSubmit('merchantConfig')">提交</el-button>
        <el-button v-if="!isEdit" @click="resetForm('merchantConfig')">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
  import { fetchList, addMerchantConfig, updateMerchantConfig, getMerchantConfigById } from '@/api/system/merchantConfig';
  import SingleUpload from '@/components/Upload/singleUpload';

  const defaultProductCate = {
    id: '',
    shopName: '',
    phone: '',
    logo: '',
    address: '',
    openTime: '',
    notes: '',
    status: ''
  };
  export default {
    name: "HomeMerchantConfigDetail",
    components: { SingleUpload },
    props: {
      isEdit: {
        type: Boolean,
        default: false
      }
    },
    data() {
      return {
        merchantConfig: {
        },
        rules: {
          shopName: [
            { required: true, message: '请输入店铺名称', trigger: 'blur' },
            { min: 2, max: 140, message: '长度在 2 到 140 个字符', trigger: 'blur' }
          ]
        }
      }
    },
    created() {
      if (this.isEdit) {
          getMerchantConfigById(this.$route.query.id).then(response => {
          this.merchantConfig = response.data;
        });
      } else {
        this.merchantConfig = Object.assign({}, defaultProductCate);
      }
    },
    methods: {
        getHomeMerchantConfig() {
            fetchList().then(response => {
                this.merchantConfig = response.data;
            });
        },
      onSubmit(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.$confirm('是否提交数据', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
              type: 'warning'
            }).then(() => {
              if (this.isEdit) {
                  const  param = {
                      id: this.merchantConfig.id,
                      shopName: this.merchantConfig.shopName,
                      phone: this.merchantConfig.phone,
                      logo: this.merchantConfig.logo,
                      address: this.merchantConfig.address,
                      openTime: this.merchantConfig.openTime,
                      notes: this.merchantConfig.notes,
                      status: this.merchantConfig.status

                  }
                  updateMerchantConfig(param).then(response => {
                      console.log(response)
                      this.$message({
                          message: '修改店铺设置成功',
                          type: 'success',
                          duration: 1000
                      });
                  this.$router.back();
                });
              } else {
                  addMerchantConfig(this.merchantConfig).then(response => {
                  this.resetForm(formName);
                      this.$router.back();
                });
              }
            });
          } else {
            this.$message({
              message: '验证失败',
              type: 'error',
              duration: 1000
            });
            return false;
          }
        });
      },
      resetForm(formName) {
        this.merchantConfig = Object.assign({}, defaultProductCate);
        this.getHomeMerchantConfig();
      }

    }
    // filters: {
    //   filterLabelFilter(index) {
    //     if (index === 0) {
    //       return '筛选属性：';
    //     } else {
    //       return '';
    //     }
    //   }
    // }
  }
</script>

<style scoped>

</style>
